import { UpdatePwd, SendUserCode } from "@/api";

let codeMaxSecond = 0;
let codeInterval = null;

const checkPwd = (password) => {
    if (isEmpty(password)) {
        Message().warning("请输入新密码");
        return false;
    } else if (
        !/^(?![a-zA-Z]+$)(?![A-Z0-9]+$)(?![A-Z\\W_]+$)(?![a-z0-9]+$)(?![a-z\\W_]+$)(?![0-9\\W_]+$)[a-zA-Z0-9\\W_]{8,20}$/.test(
            password
        )
    ) {
        Message().warning("密码需要包含大小写字母、数字、特殊字符");
        return false;
    }

    return true;
};

export const useContent = () => {
    const phone = ref("");
    const code = ref("");
    const oldPassword = ref("");
    const newPassword = ref("");
    const codeText = ref("获取验证码");
    const userStore = useUserStore();
    const pageStore = usePageStore();

    const onSendCodeSuccess = () => {
        const _func = () => {
            if (codeMaxSecond === 0) {
                codeText.value = `获取验证码`;
                clearInterval(codeInterval);
            } else {
                codeText.value = `${codeMaxSecond--}s`;
            }
        };
        codeMaxSecond = 120;
        codeInterval = setInterval(_func, 1000);
        _func();
    };

    const onSendCode = async () => {
        if (codeMaxSecond > 0) return;
        if (isEmpty(phone.value)) {
            Message().warning("请输入手机号");
        } else if (!isPhone(phone.value)) {
            Message().warning("请输入正确的手机号");
        } else {
            const res = await SendUserCode({
                phone: phone.value,
                event: "changepwd",
            });

            if (res.code == 200) {
                Message().success("发送成功");
                onSendCodeSuccess();
            } else {
                Message().error(res.msg);
            }
        }
    };

    const onSubmit = async () => {
        if (isEmpty(phone.value)) {
            Message().warning("请输入手机号");
            return false;
        }

        if (!isPhone(phone.value)) {
            Message().warning("请输入正确的手机号");
            return false;
        }

        if (isEmpty(code.value)) {
            Message().warning("请输入验证码");
            return false;
        }

        if (isEmpty(oldPassword.value)) {
            Message().warning("请输入旧密码");
            return false;
        }

        if (!checkPwd(newPassword.value)) {
            return false;
        }

        const res = await UpdatePwd({
            phone: phone.value,
            old_password: oldPassword.value,
            new_password: newPassword.value,
            code: code.value,
        });

        if (res.code == 200) {
            userStore.CLEAR_USERINFO();

            if (pageStore.isMobile) {
                Message().success({
                    content: "修改成功",
                    duration: 1500,
                    onClosed() {
                        navigateTo("/login.html");
                    },
                });
            } else {
                Message().success({
                    message: "修改成功",
                    duration: 1500,
                    onClose() {
                        navigateTo("/login.html");
                    },
                });
            }
        } else {
            Message().error(res.msg);
        }
    };

    return {
        phone,
        code,
        oldPassword,
        newPassword,
        codeText,
        onSendCode,
        onSubmit,
    };
};
